//
//  TeamFansListVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/30.
//

import UIKit

class TeamFansListVC: ZMViewController, UITableViewDelegate, UITableViewDataSource, ListRequestObjct {
    //MARK: --------------------属性--------------------
    // 1 间接 0 直接
    var type = 0
    
    var listData = [MineModels.ACUserModel]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        tableView
    }
    //MARK: --------------------UI控件--------------------
    @IBOutlet weak var titleLabel: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = type == 0 ? "直推粉丝" : "间推粉丝"
        titleLabel.text = type == 0 ? "直推粉丝（0）" : "间推粉丝（0）"
        tableView.delegate = self
        tableView.dataSource = self
        tableView.zm.register(nibName: "TeamFansListCell", reuserId: "TeamFansListCell")
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        
        addRefresh(scrollView: tableView) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
        addNodataScrollView(scrollView: tableView)
        emptyTitle = "暂无数据"
        emptyImg = "nodata"
        
        tableView.mj_header?.beginRefreshing()
    }
    
    func addConstraints() {
        
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    func loadList() {
        let pa = ["limit" : pageSize, "page" : page, "isDirect" :  type == 0] as [String : Any]
        ZMUtil.listRequest(type: MineModels.ACUserModel.self, api: MineAPI.teamInviteInfo(pa)) { resp in
            if let real = resp.realResp {
                let total = JSON(real)["total"].intValue
                self.titleLabel.text = self.type == 0 ? "直推粉丝（\(total)）" : "间推粉丝（\(total)）"
            }
            self.listRequest(resp)
        }
    }
    
    //MARK: --------------------delegate--------------------
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        tableView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TeamFansListCell") as! TeamFansListCell
        let u = listData[indexPath.row]
        cell.userView.name.text = u.nickname
        
        cell.userView.wechat.isHidden = u.wechatNo.count == 0
        
        cell.userView.onWxClick = { [weak self] in
            let v = ZMUtil.loadNibViewFullScreen(nibName: ServiceWx.self)
            v.wxLabel.text = u.wechatNo
            v.title.text = "微信号"
            v.tijkfwx.isHidden = true
            APPWindow?.addSubview(v)
        }
        
        
        cell.userView.avatar.zm.setImage(urlStr: u.avatar, placeholder: "def_user_img")
        cell.userView.phone.text = u.phone
        cell.userView.monthOrder.text = "\(u.monthOrder)"
        cell.userView.fans.text = "\(u.fansNum)"
        cell.userView.invite.text = "\(u.inviteNum)"
        cell.userView.totalProfit.text = u.totalProfit.zm.toString(keep: 4)
        cell.userView.todayProfit.text = u.todayProfit.zm.toString(keep: 4)
        cell.userView.monthProfit.text = "\(u.moneyProfit.zm.toString(keep: 4))"
        cell.userView.teamBtn.isHidden = true
        cell.userView.vipIcon.isHidden = u.serviceProviderLevel == 0

        return cell
    }
    
    
}
